package com.takeeasy.platform.control;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.takeeasy.platform.dataobject.RequestDataObject;
import com.takeeasy.platform.dataobject.ResponseDataObject;
import com.takeeasy.platform.persistence.Page;

public abstract class BaseController implements IController {

	private RequestDataObject requestDataObject;
	private ResponseDataObject responseDataObject;
	private HttpServletRequest httpRequest;
	private HttpServletResponse httpResponse;

	public RequestDataObject getRequestDataObject() {
		return requestDataObject;
	}

	public void setRequestDataObject(RequestDataObject requestDataObject) {
		this.requestDataObject = requestDataObject;
	}

	public ResponseDataObject getResponseDataObject() {
		return responseDataObject;
	}

	public void setResponseDataObject(ResponseDataObject responseDataObject) {
		this.responseDataObject = responseDataObject;
	}

	public HttpServletRequest getHttpRequest() {
		return httpRequest;
	}

	public void setHttpRequest(HttpServletRequest httpRequest) {
		this.httpRequest = httpRequest;
	}

	public HttpServletResponse getHttpResponse() {
		return httpResponse;
	}

	public void setHttpResponse(HttpServletResponse httpResponse) {
		this.httpResponse = httpResponse;
	}

	@Override
	public void parseRequest() {
		this.requestDataObject = new RequestDataObject();
		requestDataObject.setRequestURI(httpRequest.getRequestURI());
		Map<String, String[]> params = httpRequest.getParameterMap();
		Map<String, String> paramMap = new HashMap<>();
		for (Entry<String, String[]> paramsEntries : params.entrySet()) {
			String key = paramsEntries.getKey();
			String value = paramsEntries.getValue()[0];
			if (key.equals("page")) {
				this.wrapPage(value);
			} else if (key.equals("submit")) {
				requestDataObject.setSubmitData(value);
			} else if (key.equals("params")) {
				this.wrapParams(value, paramMap);
			} else {
				paramMap.put(key, value);
			}
		}
		String method = this.httpRequest.getMethod();
		requestDataObject.setRequestMethod(method);
		requestDataObject.setParams(paramMap);
		requestDataObject.setSession(httpRequest.getSession(true));
		try {
			requestDataObject.setRequestBody(httpRequest.getReader());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void wrapParams(String value, Map<String, String> paramMap) {
		if (value == null || "".equals(value)) {
			return;
		} else {
			ObjectMapper mapper = new ObjectMapper();
			try {
				@SuppressWarnings("unchecked")
				HashMap<String, String> params = mapper.readValue(value, HashMap.class);
				paramMap.putAll(params);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void wrapPage(String value) {
		if (value == null || "".equals(value)) {
			return;
		}
		ObjectMapper mapper = new ObjectMapper();
		try {
			Page page = mapper.readValue(value, Page.class);
			requestDataObject.setPage(page);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void parseResponse() {

	}

}
